﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Platform.ObjectModel;
using System.Diagnostics;
using System.Linq.Expressions;

namespace Platform.Storage.Impl
{
	internal sealed class MemberAccessTranslation
	{

		public Expression RelationExpression
		{ get; set; }

		public Type RelationType
		{ get; set; }

		public DataObjectName RelationSchemaName
		{ get; set; }

		public Expression FieldExpression
		{ get; set; }

		internal string GetRelationFieldName( DataObjectName parentName )
		{
			Debug.Assert( parentName != null, "O nome do schema não foi definido para a relação." );

			if ( this.HasRelation )
			{
				string propertyName = this.RelationExpression.GetPropertyName();
				return parentName.GroupWith( propertyName );
				//return parentName.GroupWithName );
			}
			else
			{
				return String.Empty;
			}
		}

		internal string GetFieldName( DataObjectName parentName, string propertyName )
		{
			Debug.Assert( parentName != null, "O nome do schema não foi definido para o campo." );
			Debug.Assert( this.FieldExpression != null, "Não achou o campo para fazer a pesquisa." );

			//	string propertyName = this.FieldExpression.GetPropertyName();

			if ( this.HasRelation )
			{
				string relationFieldName = this.RelationExpression.GetPropertyName();

				return this.RelationSchemaName.GroupWith( propertyName.Substring( relationFieldName.Length + 1 ) );
			}
			else
			{
				return parentName.GroupWith( propertyName );
			}
		}

		public bool HasRelation
		{ get { return this.RelationExpression != null && this.RelationSchemaName != null; } }
	}
}
